package com.scyshkj.university.wechat;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * spring beans的工具类<br>
 * 主要用于在未被spring管理的类中获取spring的bean
 */
@Service
public class BeanProvider implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public BeanProvider() {
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        BeanProvider.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext == null ? null : applicationContext.getBean(clazz);
    }
}